package testing;

import game.GameBase;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;

import GUI.anim.Animation;
import GUI.anim.Animator;

public class AnimTest extends JFrame {
	private static final long serialVersionUID = -2372826855292019889L;
	
	private Animation anim = null;
	private Animator anr = null;
	private BufferedImage backBuffer = null;
	
	public AnimTest(String tag)
	{
		super("Animation test");
		GameBase.get();
		anim = GameBase.getAnimationByTag(tag);
		if(anim != null)
		{
			anr = new Animator(anim);
			anr.play();
			System.out.println("Animation found, frames : " + anim.getFrameCount());
		}
		else
		{
			System.out.println("Animation not found : " + tag);
		}
		
		backBuffer = new BufferedImage(640, 480, BufferedImage.TYPE_4BYTE_ABGR);
		setSize(320, 240);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
		setResizable(false);
		
		Thread t = new Thread(new Runnable() {
			
			@Override
			public void run() {
				while(true)
				{
					if(isVisible())
						repaint();
					try {
						Thread.sleep(20);
					} catch (InterruptedException e) {}
				}
			}
		});
		t.start();
	}
	
	public void paint(Graphics gg)
	{
		if(backBuffer != null)
		{
			Graphics2D g = backBuffer.createGraphics();
			g.setColor(Color.white);
			g.fillRect(0, 0, this.getWidth(), this.getHeight());
			g.setColor(Color.yellow);
			g.drawLine(this.getWidth() / 2, 0, this.getWidth() / 2, this.getHeight());
			g.drawLine(0, this.getHeight() / 2, this.getWidth(), this.getHeight() / 2);
			if(anr != null)
			{
				anr.drawFrame(g, this.getWidth() / 2, this.getHeight() / 2);
				g.setColor(Color.red);
				g.drawString("Frame #" + anr.getFrame(), 10, 50);
			}
			
			gg.drawImage(backBuffer, 0, 0, null);
		}
	}

	public static void main(String[] args) {
		new AnimTest("DoomGuyD");
	}

}
